package ua.org.nuos.sdms.clientgui.client.handlers;

import com.vaadin.terminal.ExternalResource;
import ua.org.nuos.sdms.clientgui.client.context.Messages;
import ua.org.nuos.sdms.clientgui.client.controller.GlobalBoard;
import ua.org.nuos.sdms.clientgui.client.events.CompleteUserRegistrationEvent;
import ua.org.nuos.sdms.clientgui.client.events.ErrorMessageEvent;
import ua.org.nuos.sdms.clientgui.client.listeners.CompleteUserRegistrationListener;
import ua.org.nuos.sdms.clientgui.server.AppProxy;
import ua.org.nuos.sdms.middle.util.exception.InvalidCodeException;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: dio
 * Date: 26.02.12
 * Time: 21:45
 * To change this template use File | Settings | File Templates.
 */
public class CompleteUserRegistrationHandler implements CompleteUserRegistrationListener {
    @Override
    public void complete(CompleteUserRegistrationEvent event) {
        String code = event.getCode();
        try {
            AppProxy.getInstance().getServiceLocator().getRegistrationBean().completeRegisterUser(code);
            AppProxy.getInstance().getAppTools().getMainWindow()
                    .open(new ExternalResource(AppProxy.getInstance().getServiceLocator().getConfiguration().getClientGuiHost()));
        } catch (InvalidCodeException e) {
            Logger.getAnonymousLogger().log(Level.WARNING, "can not complete registration", e);
            AppProxy.getInstance().getController(GlobalBoard.ID).fire(new ErrorMessageEvent(Messages.COMPLETE_REGISTRATION_ERROR));
        }
    }
}
